/* Copyright 2009 Palm, Inc.  All rights reserved. */

CallforwardingAssistant = Class.create({
    initialize: function(updateCallback){
		this.updateCallback = updateCallback;
    },
    
    setup: function(){
        this.callForwardingNumbersList = $('callForwardingNumbersList');
        this.callForwardingNumbers = [{
			'label': $L('None'),
            'value': 'none'
        }];
        
        var numbers = Settings.callForwarding.numberList.uniq();
        
        numbers.each((function(number){
            this.callForwardingNumbers.push({
                'label': number,
				'value': number
            });
        }).bind(this));
        this.callForwardingNumbersModel = {
            listTitle: $L("Call forwarding numbers"),
            items: this.callForwardingNumbers
        };
        this.controller.setupWidget('callForwardingNumbersList', {
            itemTemplate: 'callforwarding/listitem',
            listTemplate: 'callforwarding/listcontainer',
            addItemLabel: $L("Add new number"),
			swipeToDelete: true
        }, this.callForwardingNumbersModel);
        
        
        this.callForwardingNumbersList.observe(Mojo.Event.listTap, this.listClickHandler.bindAsEventListener(this));
        this.callForwardingNumbersList.observe(Mojo.Event.listAdd, this.listAddHandler.bindAsEventListener(this));
        this.callForwardingNumbersList.observe(Mojo.Event.listDelete, this.listDeleteHandler.bindAsEventListener(this));
        this.callForwardingNumbersList.observe(Mojo.Event.listChange, this.listChangeHandler.bindAsEventListener(this));
        
    },
    
    listClickHandler: function(event){
    
        var number = event.item.label;
        
        if (number != "") {
            Mojo.Controller.stageController.popScene();
			
            Mojo.Controller.stageController.delegateToSceneAssistant("updateCallForwardingList");
        }
    },
    
    listAddHandler: function(event){
        // Warning: By not checking which model we're modifying here, we implicitly assume that they share the same structure.
        event.model.items.push({
			'label': '',
            'value': ''
        });
        this.controller.modelChanged(event.model, this);
        this.callForwardingNumbersList.mojo.focusItem(event.model.items[event.model.items.length - 1]);
    },
    
    listDeleteHandler: function(event){
        Mojo.log("deleting '" + event.item.value);
		this.callForwardingNumbersModel.items.splice(this.callForwardingNumbersModel.items.indexOf(event.item), 1);
		this.updateCallForwardingNumbersCache();
    },
    
    
    // Called for Mojo.Event.listChange events, which are sent when a 'change' event comes from a list item.
    // Saves the new value into the model.
    listChangeHandler: function(event){
      
        if (event.originalEvent.target.tagName == "INPUT") {
			if (this.validateNumber(event.originalEvent.target.value)) {
				event.item.value = event.originalEvent.target.value;
				event.item.label = event.originalEvent.target.value;
				Mojo.log("Change called.  Number is now: " + event.item.label + " " + Object.toJSON(this.callForwardingNumbers));
				this.updateCallForwardingNumbersCache();
			}
			
        }
    },
	
	//we can add more checks later, for now check for empty strings
	validateNumber: function(number) {
		if (number == "") {
			return false;
		}
		return true;
	},
	
	
	updateCallForwardingNumbersCache: function(){
		//update the main cached call forwarding numbers list
		Settings.callForwarding.numberList.clear();
		this.callForwardingNumbers.each(function(item) {
			if (item.value != 'none') {
				Settings.callForwarding.numberList.push(item.label);
			}
		});
		this.updateCallback();
	},
	
});
